package com.ww.springboot.boot.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ww.springboot.boot.config.RedisUtils;
import com.ww.springboot.boot.service.IRedisService;
import com.ww.springboot.boot.service.IRedisService2;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 *
 * @author wanwei
 * @since 2021-08-02 11:56
 */
@Service
@AllArgsConstructor
public class RedisServiceImpl implements IRedisService {


    private final RedisUtils redisUtils;

    private final IRedisService2 redisService2;

    public static String secKill = "redis:plus:secKill:";

    public static Map<String, String> map = new HashMap<String, String>() {{
        put("1", "1");
        put("2", "2");
        put("3", "3");
        put("4", "4");
        put("5", "5");
        put("6", "6");
        put("7", "7");
        put("8", "8");
        put("9", "9");
        put("10", "10");
    }};

    /**
     * 先从redis中取  如果取不到 则从数据库中取
     * 如果数据库取到了 则写到redis中去
     * <p>
     * 设定：数据库操作 如果每秒执行了十次 则抛异常-》模拟数据库被打爆
     */
    @Override
    public void secKill(String name) {
        String str = redisUtils.get(secKill + name);
        if (ObjectUtil.isEmpty(str)) {
            redisService2.getFromDataSource(name);
        }
    }

}
